// fluidAppDlg.cpp : implementation file
//

#include "stdafx.h"
#include "fluidApp.h"
#include "fluidAppDlg.h"
#include ".\fluidappdlg.h"
#include "SnapshotManager.h"
//#ifdef _DEBUG
//#define new DEBUG_NEW
//#endif
//#include "densityField.h"
#include "Scene.h"
#include "DecoRigidBody.h"
#include "DecoFluidBody.h"
#include "DecoArticulatedBody.h"
// CAboutDlg dialog used for App About


class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CfluidAppDlg dialog



CfluidAppDlg::CfluidAppDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CfluidAppDlg::IDD, pParent)
	, panelMgr(this)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CfluidAppDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO_OBJTYPES, m_comboObjTypes);
}

BEGIN_MESSAGE_MAP(CfluidAppDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_WM_CREATE()
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_BUTTON_ADDOBJECT, OnBnClickedButtonAddobject)
	ON_BN_CLICKED(IDC_BUTTON_VOXELIZE, OnBnClickedButtonVoxelize)
	ON_BN_CLICKED(IDC_BUTTON_VIEW, OnBnClickedButtonView)
	ON_BN_CLICKED(IDC_BUTTON_VIEW2, OnBnClickedButtonView2)
	ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON_SAVE, OnBnClickedButtonSave)
	ON_BN_CLICKED(IDC_BUTTON_LOAD, OnBnClickedButtonLoad)
END_MESSAGE_MAP()


// CfluidAppDlg message handlers
const static UINT OBJ_TYPE_NUM = 5;

const static std::string ObjTypeStrings[OBJ_TYPE_NUM] = 
{
	"Solid", "Fluid", "Sprayer", "Freer", "Articulated"
};

BOOL CfluidAppDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here
	panelMgr.addPanel(&mainDlg, IDD_DIALOG_MAIN);
	panelMgr.init();
	panelMgr.showPanel(0);

	for(UINT i = 0; i < OBJ_TYPE_NUM; ++i)
        m_comboObjTypes.AddString(ObjTypeStrings[i].c_str());
	m_comboObjTypes.SetCurSel(0);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CfluidAppDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CfluidAppDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CfluidAppDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

int CfluidAppDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CDialog::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  Add your specialized creation code here

	return 0;
}

void CfluidAppDlg::OnDestroy()
{
	CDialog::OnDestroy();

	// TODO: Add your message handler code here
}

void CfluidAppDlg::OnBnClickedButtonAddobject()
{
	DecoSceneObject * pObj = NULL;

	int nCurSel = m_comboObjTypes.GetCurSel();
	if (2 == nCurSel)//sprayerT
	{
		pObj = DecoSprayerObject::CreateFromMesh("models\\sprayer.ase");
	}
	else if (3 == nCurSel)//freerT
	{
		//pObj = DecoFreerObject::CreateFromMesh("models\\freer.ase");
		char szFilters[]=
			"ASE Files (*.ase)|*.ase|OBJ Files(*.obj)|*.obj|All Files (*.*)|*.*||";

		// Create an Open dialog; the default file name extension is ".my".
		CFileDialog fileDlg (TRUE, "ase", "*.ase",
			OFN_FILEMUSTEXIST| OFN_HIDEREADONLY, szFilters, this);

		// Display the file dialog. When user clicks OK, fileDlg.DoModal() 
		// returns IDOK.
		if( fileDlg.DoModal ()==IDOK )
		{
			CString pathName = fileDlg.GetPathName();
			switch(nCurSel)
			{
			case 0://solidT
				{
					pObj = DecoRigidObject::CreateFromMesh(std::string(pathName));
				}
				break;
			case 1://fluidT
				{
					pObj = DecoFluidObject::CreateFromMesh(std::string(pathName));
				}
				break;
			case 3://FreerT
				{
					pObj = DecoFreerObject::CreateFromMesh(std::string(pathName));
				}
			}
		}
	}
	else if (4 == nCurSel)
	{
		char szFilters[]=
			"SKEL Files (*.skel)|*.skel||";

		CFileDialog fileDlg (TRUE, "skel", "*.skel",
			OFN_FILEMUSTEXIST| OFN_HIDEREADONLY, szFilters, this);

		if( fileDlg.DoModal ()==IDOK )
		{
			CString pathName = fileDlg.GetPathName();
			pObj = DecoArticulatedObject::CreateFromMesh(std::string(pathName));
		}

	}
	else
	{
		// TODO: Add your control notification handler code here
		// szFilters is a text string that includes two file name filters:
		// "*.my" for "MyType Files" and "*.*' for "All Files."
		char szFilters[]=
			"ASE Files (*.ase)|*.ase|OBJ Files(*.obj)|*.obj|All Files (*.*)|*.*||";

		// Create an Open dialog; the default file name extension is ".my".
		CFileDialog fileDlg (TRUE, "ase", "*.ase",
			OFN_FILEMUSTEXIST| OFN_HIDEREADONLY, szFilters, this);

		// Display the file dialog. When user clicks OK, fileDlg.DoModal() 
		// returns IDOK.
		if( fileDlg.DoModal ()==IDOK )
		{
			CString pathName = fileDlg.GetPathName();
			switch(nCurSel)
			{
			case 0://solidT
				{
					pObj = DecoRigidObject::CreateFromMesh(std::string(pathName));
				}
				break;
			case 1://fluidT
				{
					pObj = DecoFluidObject::CreateFromMesh(std::string(pathName));
				}
				break;
			}
		}
	}

	if(pObj != NULL)
	{
		pObj->Rotate(AXIS_X, (FLOAT)(-PI)/2);
		mainDlg.scene->AddSceneObject(pObj);
		Invalidate(FALSE);
	}
}

void CfluidAppDlg::OnBnClickedButtonVoxelize()
{
	// TODO: Add your control notification handler code here
	mainDlg.scene->Voxelize();
#ifdef _DENSITY
	mainDlg.scene->RenderVolume();
	mainDlg.scene->RI->SaveToImage("tmpPic\\voxelization.jpg");
#endif
	Invalidate(FALSE);
}

void CfluidAppDlg::OnBnClickedButtonView()
{
	// TODO: Add your control notification handler code here
	//mainDlg.
	mainDlg.ToggleModelView();
}

void CfluidAppDlg::OnBnClickedButtonView2()
{
	// TODO: Add your control notification handler code here
	mainDlg.ToogleGridView();
}

void CfluidAppDlg::OnBnClickedButton1()
{
	// TODO: Add your control notification handler code here
	mainDlg.StartTimer();
}


void CfluidAppDlg::OnBnClickedButtonSave()
{
#ifdef MFC_ENABLED
	mainDlg.scene->SaveToXML("c:\\test.xml");
#endif
}

void CfluidAppDlg::OnBnClickedButtonLoad()
{
	char szFilters[]=
		"DAT Files (*.dat)|*.dat|All Files (*.*)|*.*||";

	// Create an Open dialog; the default file name extension is ".my".
	CFileDialog fileDlg (TRUE, "dat", "*.dat",
		OFN_FILEMUSTEXIST| OFN_HIDEREADONLY, szFilters, this);

	// Display the file dialog. When user clicks OK, fileDlg.DoModal() 
	// returns IDOK.
	if( fileDlg.DoModal ()==IDOK )
	{
		CString pathName = fileDlg.GetPathName();
		DecoSnapshotManager::GetSingleton()->RecoverFromSnapshot(std::string(pathName), mainDlg.scene);
	}

	Invalidate(FALSE);
}

